//
//  ZMNewVersionView.swift
//  TravelForDriver
//
//  Created by 浪仔 on 2020/9/2.
//  Copyright © 2020 智蚂优服. All rights reserved.
//

import UIKit


class ZMNewVersionView: XMBaseView {
    
    @IBOutlet weak var noDisView: UIView!
    @IBOutlet weak var bgImg: UIImageView!
    /// 版本描述
    @IBOutlet weak var versionNo: UILabel!
    @IBOutlet weak var desc: UITextView!
    @IBOutlet weak var descHeight: NSLayoutConstraint!
    /// 不在提醒
    @IBOutlet weak var noTipsBtn: UIButton!
    /// 跳过
    @IBOutlet weak var nextBtn: UIButton!
    /// 升级
    @IBOutlet weak var upgradeBtn: UIButton!
    
    var info = AppVersionModel()
    let disposeBag = DisposeBag()
    
    var onNext : (()->())?
    
    public class func showVersion(_ info: AppVersionModel, onNext : (()->())? = nil) {
        let view = ZMUtil.loadNibView(nibName: ZMNewVersionView.self)
        view.frame = SCREEN_BOUNDS
        view.noTipsBtn.isHidden = info.isForceUpdate
        view.nextBtn.isHidden = info.isForceUpdate
        view.versionNo.text = "新功能"
        view.desc.text = info.desc
        view.info = info
        view.onNext = onNext
        APPWindow?.addSubview(view)
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        close666TouchOutSideRemove = true
        bgImg.image = getImage("upgrade_bg.png")
        noTipsBtn.setImage(getImage("select.png"), for: .normal)
        noTipsBtn.setImage(getImage("select_s.png"), for: .selected)
        upgradeBtn.backgroundColor = Style.mainColor
        upgradeBtn.setTitleColor(.white, for: .normal)
        bindAction()
    }
    
    func bindAction() {
        desc.rx.observeWeakly(CGSize.self, "contentSize").subscribe(onNext: { [unowned self] (change) in
            if let height = change?.height {
                self.descHeight.constant = (height > 120 ? 120 : height)
            }
        }).disposed(by: disposeBag)
    }
    
    @IBAction func noTipsBtnClick(_ sender: UIButton) {
        sender.isSelected = !sender.isSelected
    }
    
    // 立即升级点击
    @IBAction func updateBtnClick(_ sender: Any) {
        UserDefaults.standard.set(nil, forKey: "VersionInfo")
        if let url = URL(string: info.url) {
            if #available(iOS 10, *) {
                UIApplication.shared.open(url)
            } else {
                UIApplication.shared.openURL(url)
            }
        } else {
//            if let url = URL(string: "itms-apps://itunes.apple.com/app/id\(APPID)") {
//                if #available(iOS 10, *) {
//                    UIApplication.shared.open(url)
//                } else {
//                    UIApplication.shared.openURL(url)
//                }
//            }
        }
    }
    
    @IBAction func nextBtnClick(_ sender: Any) {
        info.isTip = noTipsBtn.isSelected
        UserDefaults.standard.set(info.toJSONString(), forKey: "VersionInfo")
        UserDefaults.standard.synchronize()
        self.removeFromSuperview()
        if let onNext = onNext {
            onNext()
        }
    }
    
    func getImage(_ name: String) -> UIImage {
        let fileName = "ZMNewVersion.bundle/\(name)"
        if let image = UIImage(named: fileName) {
            return image
        }
        return UIImage()
    }
}

